--#include "data\config\equip\EquipRecoveryCfg.lua" once

--普通装备回收
function EquipRecover(sysarg, args)
	--local nIdx = args[2]						-- 等级索引(不用这个字段了，加了自动回收功能对不上)
	local btRecoveryType = args[3]				-- 是否为vip随身回收, 1是, 0否
	
	if btRecoveryType == 1 then			--随身回收
		if GetVipGradePrivValue( sysarg, enVipPriv_EquipRecycle ) <= 0 then 			--是否开放装备随身回收
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.Vip003, ttFlyTip)
			return
		end
	end
	
	local openDays = System.getDaysSinceOpenServer()
	local totalBindCoin = 0	-- 回收装备获得的金币
	local totalExp = 0			-- 回收装备获得的总经验
	local totalChip1 = 0		-- 回收装备获得的碎片1
	local totalChip2 = 0		-- 回收装备获得的碎片2
	local totalYB = 0
	
	local nItemCount = args[4]		-- 装备数量
	local pos		= args[5]				-- 位置：0背包1寻宝仓库
	local packet 	= args[6]				-- 装备guid列表
	local nEquipItemPtr = {}   --存储物品指针用来最后删除
	local nEquipGuidList = {}  --把取出的每个物品的GUID储存,用来检测是否已存在一样的,有则是通过非法手段来进行
	local awardList = {}
	
	local funList =
	{
		[0] =
		{
			getBagItemPtrByGuid = Item.getBagItemPtrByGuid,
			removeItemByPtr		= Actor.removeItemByPtr,
		},
		[1] =
		{
		    getBagItemPtrByGuid = Dmkj.getItemPtrByGuid,
			removeItemByPtr		= Dmkj.removeItemByPtr,
		}
	}

	local count = 0
	for i = 1,nItemCount do
		local itemGuid = DataPack.readDouble(packet)
		if not CheckSameGuid(nEquipGuidList, itemGuid) then 
			return 
		end
	
		local itemPtr = funList[pos].getBagItemPtrByGuid(sysarg, itemGuid)
		if not itemPtr then
			return
		end
		
		-- 获取装备是否可回收属性
		local nItemId = Item.getItemId(itemPtr)
		local config = GetEquipLevelCfg(nItemId)
		if config then
			if config.openSvrDay ==  0 or openDays >= config.openSvrDay then
				local denyReclaim = Item.getItemPropertyById(nItemId, Item.ipItemDenyReclaim)
				if denyReclaim > 0 then
					Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.zf248, Item.getItemName(nItemId)), ttFlyTip)
					return
				end
				
				-- 统计奖励
				local awards = GetEquipReclaimAward(sysarg, config, nItemId)
				
				if awards ~= nil then
					totalBindCoin = totalBindCoin + awards[3]
					totalExp = totalExp + awards[2]
					totalChip1 = totalChip1 + (awards[4] or 0)
					totalChip2 = totalChip2 + (awards[5] or 0)
					totalYB = totalYB + (awards[6] or 0)
				end
				
				-- 保存装备指针
				table.insert(nEquipGuidList, itemGuid)
				table.insert(nEquipItemPtr, itemPtr)
				count = count + 1
			end
		end
	end
	
	if count <= 0 then
		return
	end
	
	local recoveryCount = 0
	-- 删除要回收的装备
	local nResult = true
	for _, itemPtr in ipairs(nEquipItemPtr or {}) do
		if funList[pos].removeItemByPtr(sysarg, itemPtr, 1, true, Lang.ScriptTips.EquipRec001, 1002) ~= 1 then
			nResult = false
			break
		end
		recoveryCount = recoveryCount + 1
	end
	
	if not nResult then
		return
	end
	
	if totalBindCoin > 0 then
		Actor.ChangeMoney(sysarg, mtBindCoin, totalBindCoin, GameLog.cEquipRecyclingAward, Lang.ScriptTips.EquipRec001)
	end
	
	if totalYB  > 0 then
		Actor.giveAward(sysarg, 10, 0, totalYB, 0, 0, 0, 0, GameLog.cEquipRecyclingAward, Lang.ScriptTips.EquipRec001)
	end


	if totalExp > 0 then
		local addExpRate = Actor.getConcurrentVipPriv( sysarg, enConcVipPriv_RecycleEquipAddExpRate )	--特权加成（百分比）
		local newTotalExp = totalExp + ( totalExp * addExpRate ) /100
		Actor.addExp(sysarg, newTotalExp, GameLog.cEquipRecyclingAward, 0)
		Hero.addHeroExp(sysarg, newTotalExp, GameLog.cEquipRecyclingAward)
		--print("EquipRecover, addExpRate="..addExpRate..", totalExp="..totalExp..", newTotalExp="..newTotalExp)
	end
	
	if totalChip1 > 0 then
		Actor.giveAward(sysarg, 22, 0, totalChip1, 0, 0, 0, 0, GameLog.cEquipRecyclingAward, Lang.ScriptTips.EquipRec001)
	end
	
	if totalChip2 > 0 then
		Actor.giveAward(sysarg, 25, 0, totalChip2, 0, 0, 0, 0, GameLog.cEquipRecyclingAward, Lang.ScriptTips.EquipRec001)
	end

	SendRecoveryAwardData(sysarg, 1, mtBindCoin, totalExp, totalChip1, totalChip2, recoveryCount)

	--成就事件
	Actor.triggerAchieveEvent(sysarg, 45, 1, recoveryCount)
end

--判断两个物品的GUID是否一样, 返回true不一样, false有一样的
function CheckSameGuid(GuidList, Guid)
	for i = 1,table.getn(GuidList) do
		if Item.IsGuidSame(GuidList[i], Guid) == true then
			return false
		end
	end
	return true
end

-- 下发回收获得的奖励数据
function SendRecoveryAwardData(sysarg, nresult, nBindCion, nexp, nchip1, nchip2, nCount)
	local npack = DataPack.allocPacket(sysarg, 7, 13)
	DataPack.writeByte(npack, nresult)		-- 回收结果1成功, 0失败
	DataPack.writeUInt(npack, nBindCion)	-- 获得的金币数量
	DataPack.writeUInt(npack, nexp)			-- 获得的经验
	DataPack.writeUInt(npack, nchip1)		-- 获得的碎片1
	DataPack.writeUInt(npack, nchip2)		-- 获得的碎片2
	DataPack.writeByte(npack, nCount)		-- 回收数量
	DataPack.flush(npack)
end

function GetEquipLevelCfg(nItemId)
	local needCircle = Item.getItemPropertyById(nItemId, Item.ipItemNeedCircle) or 0
    local needLevel  = Item.getItemPropertyById(nItemId, Item.ipItemActorLevel) or 0
	for i,v in ipairs(EquipRecoveryCfg) do
		if #v.equips > 0 then
			if needCircle > 0 and needCircle>= v.circleMin and needCircle <= v.circleMax then
				return v
			elseif needCircle == 0 and needLevel >= v.LevelMin and needLevel <= v.LevelMax then
				return v
			end
		end
	end
end

-- 获得回收装备的奖励
function GetEquipReclaimAward(sysarg, config, nItemId)
	local awards = {}
	for _,data in ipairs(config.equips) do
		if data[1] == nItemId then
			return data
		end
	end
	return nil
end

